<?php
/**
 * 我的余额
 *
 *
 *
 *
 * @copyright  Copyright (c) 2007-2015 ShopNC Inc. (http://www.shopnc.net)
 * @license    http://www.shopnc.net
 * @link       http://www.shopnc.net
 * @since      File available since Release v1.1
 */



defined('ByShopWWI') or exit('Access Invalid!');

class member_fundControl extends mobileMemberControl {

    public function __construct(){
        parent::__construct();
    }
	
	
	/**
     * 充值列表
     */
    public function indexOp(){
        $condition = array();
        $condition['pdr_member_id'] = $this->member_info['member_id'];
        if (!empty($_GET['pdr_sn'])) {
            $condition['pdr_sn'] = $_GET['pdr_sn'];
        }

        $model_pd = Model('predeposit');
        $list = $model_pd->getPdRechargeList($condition,20,'*','pdr_id desc');
		foreach($list as $key=>$value){
			$list[$key]['pdr_add_time_text'] = date('Y-m-d H:i:s',$value['pdr_add_time']);
		}
		$page_count = $model_pd->gettotalpage();
        output_data(array('list' => $list),mobile_page($page_count));
    }
    /**
     * 余额记录
     */
    public function predepositlogOp() {
		$model_pd = Model('predeposit');
		$page = new Page();
        $condition = array();
        $condition['lg_member_id'] = $this->member_info['member_id'];
        $list = $model_pd->getPdLogList($condition,20,'*','lg_id desc');
		$page_count = $model_pd->gettotalpage();
        output_data(array('list' => $list),mobile_page($page_count));
    }

	/**
     * 体现记录
     */
    public function pdcashlistOp() {
		$condition = array();
        $condition['pdc_member_id'] =  $this->member_info['member_id'];
        if (preg_match('/^\d+$/',$_GET['sn_search'])) {
            $condition['pdc_sn'] = $_GET['sn_search'];
        }
        if (isset($_GET['paystate_search'])){
            $condition['pdc_payment_state'] = intval($_GET['paystate_search']);
        }
        $model_pd = Model('predeposit');
        $cash_list = $model_pd->getPdCashList($condition,30,'*','pdc_id desc');
		

		$page_count = $model_pd->gettotalpage();
        output_data(array('list' => $cash_list),mobile_page($page_count));

	}
	


	/**
     * 充值卡充值
     */
	public function rechargecard_addOp() {
		if($this->member_info['member_id']){
            if(!$this->check()){
				output_error('验证码错误！');
			}
       

			$sn = (string) $_POST['rc_sn'];
			if (!$sn || strlen($sn) > 50) {
				output_error('平台充值卡卡号不能为空且长度不能大于50');
				exit;
			}
			try {
				model('predeposit')->addRechargeCard($sn, $this->member_info);
				output_data('平台充值卡使用成功');
			} catch (Exception $e) {
				output_error($e->getMessage());
				exit;
			}
		}else{
			output_error('请登录！');
		} 
	}
		

		
	/**
     * 充值卡记录列表
     */
	public function rcblogOp() {
		$model = Model();
        $list = $model->table('rcb_log')->where(array('member_id' => $this->member_info['member_id']))->page(20)->order('id desc')->select();

		$page_count = $model->gettotalpage();
        output_data(array('log_list' => $list),mobile_page($page_count));
	}

	/**
     * 我的积分 我的余额
     */
    public function my_assetOp() {
		$point = $this->member_info['member_points'];
		output_data(array('point' => $point));
	}
	protected function getMemberAndGradeInfo($is_return = false){
        $member_info = array();
        //会员详情及会员级别处理
        if($this->member_info['member_id']) {
            $model_member = Model('member');
            $member_info = $model_member->getMemberInfoByID($this->member_info['member_id']);
            if ($member_info){
                $member_gradeinfo = $model_member->getOneMemberGrade(intval($member_info['member_exppoints']));
                $member_info = array_merge($member_info,$member_gradeinfo);
                $member_info['security_level'] = $model_member->getMemberSecurityLevel($member_info);
            }
        }
        if ($is_return == true){//返回会员信息
            return $member_info;
        } else {//输出会员信息
            Tpl::output('member_info',$member_info);
        }
    }
	##############
 /**
     * 统一发送身份验证码
     */
   /* public function send_auth_codeOp() {
		$_POST['type'] ='mobile';
        if (!in_array($_POST['type'],array('mobile'))) exit();

        $model_member = Model('member');
        $member_info = $model_member->getMemberInfoByID($this->member_info['member_id'],'member_mobile');

        //发送频率验证
        $member_common_info = $model_member->getMemberCommonInfo(array('member_id'=>$this->member_info['member_id']));
        if (!empty($member_common_info['send_acode_time'])) {
            if (date('Ymd',$member_common_info['send_acode_time']) != date('Ymd',TIMESTAMP)) {
                $data = array();
                $data['send_acode_times'] = 0;
                $update = $model_member->editMemberCommon($data,array('member_id'=>$this->member_info['member_id']));
            } else {
                if (TIMESTAMP - $member_common_info['send_acode_time'] < 58) {
                    //exit(json_encode(array('state'=>'false','msg'=>'请60秒以后再次发送短信')));
					output_error('请60秒以后再次发送短信');
                } else {
                    if ($member_common_info['send_acode_times'] >= 15) {
                       // exit(json_encode(array('state'=>'false','msg'=>'您今天发送验证信息已超过15条，今天将无法再次发送')));
					   output_error('您今天发送验证信息已超过15条，今天将无法再次发送');
                    }
                }
            }
        }

        $verify_code = rand(100,999).rand(100,999);
        $model_tpl = Model('mail_templates');
        $tpl_info = $model_tpl->getTplInfo(array('code'=>'authenticate'));

        $param = array();
        $param['send_time'] = date('Y-m-d H:i',TIMESTAMP);
        $param['verify_code'] = $verify_code;
        $param['site_name'] = C('site_name');
        $subject = ncReplaceText($tpl_info['title'],$param);
        $message = ncReplaceText($tpl_info['content'],$param);
       if ($_GET['type'] == 'email') {
            $email	= new Email();
            $result	= $email->send_sys_email($member_info["member_email"],$subject,$message);
        } elseif ($_GET['type'] == 'mobile') {
            $sms = new Sms();
            $result = $sms->send($member_info["member_mobile"],$message);
        }
        if ($result) {
            $data = array();
            $update_data['auth_code'] = $verify_code;
            $update_data['send_acode_time'] = TIMESTAMP;
            $update_data['send_acode_times'] = array('exp','send_acode_times+1');
            $update = $model_member->editMemberCommon($update_data,array('member_id'=>$this->member_info['member_id']));
            if (!$update) {
               // exit(json_encode(array('state'=>'false','msg'=>'系统发生错误，如有疑问请与管理员联系')));
			  output_error('系统发生错误，如有疑问请与管理员联系');
            }
            //exit(json_encode(array('state'=>'true','msg'=>'验证码已发出，请注意查收')));
			$msg = '验证码已发出，请注意查收';
			output_data($msg);
        } else {
           // exit(json_encode(array('state'=>'false','msg'=>'验证码发送失败')));
		   output_error('验证码发送失败');
        }
    }	*/
	/*
	/**
     * 申请提现
     */
    public function pd_cash_addOp(){
        if (!empty($_POST)){
			$pdc_amount = abs(floatval($_POST['pdc_amount']));
            $model_pd = Model('predeposit');
            $model_member = Model('member');
            $member_info = $model_member->getMemberInfoByID($this->member_info['member_id']);
            //验证支付密码
            if (md5($_POST['password']) != $member_info['member_paypwd']) {
               // showDialog('支付密码错误','','error');
			   output_error('支付密码错误');
            }
            //验证金额是否足够
            if (floatval($member_info['available_predeposit']) < $pdc_amount){
               // showDialog(Language::get('predeposit_cash_shortprice_error'),'index.php?act=predeposit&op=pd_cash_list','error');
			   output_error('资金不足');
            }
           // try {
              //  $model_pd->beginTransaction();
                $pdc_sn = $model_pd->makeSn();
                $data = array();
                $data['pdc_sn'] = $pdc_sn;
                $data['pdc_member_id'] =$this->member_info['member_id'];
                $data['pdc_member_name'] = $this->member_info['member_name'];
                $data['pdc_amount'] = $pdc_amount;
                $data['pdc_bank_name'] = $_POST['pdc_bank_name'];
                $data['pdc_bank_no'] = $_POST['pdc_bank_no'];
                $data['pdc_bank_user'] = $_POST['pdc_bank_user'];
                $data['pdc_add_time'] = TIMESTAMP;
                $data['pdc_payment_state'] = 0;
                $insert = $model_pd->addPdCash($data);
                if (!$insert) {
					output_error('提现失败');
                    //throw new Exception(Language::get('predeposit_cash_add_fail'));
                }
                //冻结可用预存款
                $data = array();
                $data['member_id'] = $member_info['member_id'];
                $data['member_name'] = $member_info['member_name'];
                $data['amount'] = $pdc_amount;
                $data['order_sn'] = $pdc_sn;
                $model_pd->changePd('cash_apply',$data);
                $model_pd->commit();
				output_data('等待审核');
               // showDialog(Language::get('predeposit_cash_add_success'),'index.php?act=predeposit&op=pd_cash_list','succ','CUR_DIALOG.close()');
            //} catch (Exception $e) {
             //   $model_pd->rollback();
			//	output_error('提现失败');
               // showDialog($e->getMessage(),'index.php?act=predeposit&op=pd_cash_list','error');
           // }
        }
    }
	/**
     * 充值添加
     */
  public function recharge_addOp(){
        //if (!chksubmit()){
            //信息输出
            //self::profile_menu('recharge_add','recharge_add');
           // Tpl::showpage('predeposit.pd_add');
           // exit();
       // }
	 $_POST['pdr_amount']=10;
        $pdr_amount = abs(floatval($_POST['pdr_amount']));
        if ($pdr_amount <= 0) {
			output_error('充值金额不能小于0');
            //showMessage(Language::get('predeposit_recharge_add_pricemin_error'),'','html','error');
        }
        $model_pdr = Model('predeposit');
        $data = array();
        $data['pdr_sn'] = $pay_sn = $model_pdr->makeSn();
        $data['pdr_member_id'] = $this->member_info['member_id'];
        $data['pdr_member_name'] = $this->member_info['member_name'];
        $data['pdr_amount'] = $pdr_amount;
        $data['pdr_add_time'] = TIMESTAMP;
        $insert = $model_pdr->addPdRecharge($data);
        if ($insert) {
            //转向到商城支付页面
			
            $data= SHOP_SITE_URL . '/index.php?act=buy&op=pd_pay&pay_sn='.$pay_sn;
			output_data($data);
        }
    }

	/**
     * AJAX验证
     *
     */
	protected function check(){
        if (checkSeccode($_POST['nchash'],$_POST['captcha'])){
            return true;
        }else{
            return false;
        }
    }
}
